function hfile = bless(hfile, lev)
% BVQXfile::bless  - make object non-unwindable
%
% FORMAT:       [obj = ] bless(obj [, lev]);
%
% Input fields:
%
%       obj         valid BVQXfile object
%       lev         optional levels, if not given up to base WS
%
% Output fields:
%
%       obj         copy of input object (for convenience)

% Version:  v0.7b
% Build:    7082921
% Date:     Aug-29 2007, 9:31 PM CEST
% Author:   Jochen Weber, Brain Innovation, B.V., Maastricht, NL
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% global storage
global bvqxclup;
global bvqxcont;

% argument check
if nargin < 1 || ...
    any(~isBVQXfile(hfile))
    return;
end
if nargin < 2 || ...
    isa(lev, 'double') || ...
    numel(lev) ~= 1 || ...
    isnan(lev) || ...
    lev < 1
    lev = Inf;
else
    lev = fix(lev);
end

% iterate over object
sfile = struct(hfile);
for sc = 1:numel(sfile)
    
    % remove stack entirely
    ifile = find(bvqxclup == sfile(sc).L);
    if isinf(lev)
        bvqxcont(ifile).U = {};
        
    % remove part of stack
    else
        U = bvqxcont(ifile).U;
        if ~isempty(U)
            U(1:min(numel(U), lev)) = [];
            bvqxcont(ifile).U = U;
        end
    end
end
